/*
 * @author: sanbao
 * @Date: 2022-08-05 15:58:24
 */
import axios from 'axios';
import md5 from 'md5'
import { getToken } from '../utils/auth'
//实现code
function getTestICode() {
    const now = parseInt(String(Date.now() / 1000))
    const code = now + 'LGD_Sunday-1991'
    return {
        icode: md5(code),
        time: now
    }
}
let url: any = import.meta.env
const request = axios.create({
    baseURL: url.VITE_APP_BASE_API,
    timeout: 5000
});

request.interceptors.request.use(config => {
    // 请求头家 code 验证
    config.headers = config.headers || {}
    const {
        icode,
        time
    } = getTestICode()
    config.headers.icode = icode
    config.headers.codeType = time
    //token
    const token = getToken()
    if (token) {
        config.headers.Authorization = `Bearer ${token}`
    }
    return config
}, err => {
    return Promise.reject(err)
});

request.interceptors.response.use(res => {
    return Promise.resolve(res)
}, err => {
    return Promise.reject(err)
});

export default request